package com.junjiao.util.web;

import java.util.ArrayList;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author wanggz
 * 
 */
public class HttpClientTools {

	private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
	private static Log log = LogFactory.getLog(HttpClientTools.class);
	private static HttpClient client = new HttpClient(connectionManager);
	private static HttpClientTools http = new HttpClientTools();

	public static synchronized HttpClient getClient() {
		return client;
	}

	private HttpClientTools() {
		configureClient();
	}

	public static synchronized HttpClientTools getInstance() {
		return http;
	}

	private void configureClient() {

		/*
		 * Protocol authhttps = new Protocol(https, new
		 * AuthSSLProtocolSocketFactory( new URL(filemy.keystore), mypassword,
		 * new URL(filemy.truststore), mypassword), 8443); HttpClient client =
		 * new HttpClient(); client.getHostConfiguration().setHost(sh.12530,
		 * 8443, authhttps);
		 */

		HttpConnectionManagerParams params = connectionManager.getParams();
		params.setConnectionTimeout(HttpClientParams.connectionTimeout);
		params.setSoTimeout(HttpClientParams.soTimeout);

		/*
		 * params.setSendBufferSize(BUFFER_SIZE);
		 * params.setReceiveBufferSize(BUFFER_SIZE);
		 */

		params.setMaxTotalConnections(HttpClientParams.maxThreadsTotal);
		if (HttpClientParams.maxThreadsTotal > HttpClientParams.maxThreadsPerHost) {
			params
					.setDefaultMaxConnectionsPerHost(HttpClientParams.maxThreadsPerHost);

		} else {
			params
					.setDefaultMaxConnectionsPerHost(HttpClientParams.maxThreadsTotal);
		}

		// init header
		HostConfiguration hostConf = client.getHostConfiguration();
		ArrayList<Header> headers = new ArrayList<Header>();

		// prefer English
		headers.add(new Header("Accept-Language",
				"en-us,zh-cn,zh-tw,en-gb,en;q=0.7,*;q=0.3"));

		// prefer UTF-8
		headers.add(new Header("Accept-Charset",
				"big5,gb2312,gbk,utf-8,ISO-8859-1;q=0.7,*;q=0.7"));

		// prefer understandable formats
		headers
				.add(new Header(
						"Accept",
						"text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));

		// accept gzipped content
		headers.add(new Header("Accept-Encoding", "x-gzip, gzip"));
		hostConf.getParams().setParameter("http.default-headers", headers);

		// proxy setting
		if (HttpClientParams.useProxy) {

			hostConf.setProxy(HttpClientParams.proxyHost,
					HttpClientParams.proxyPort);
			if (HttpClientParams.ntlmUsername.length() > 0) {

				Credentials ntCreds = new NTCredentials(
						HttpClientParams.ntlmUsername,
						HttpClientParams.ntlmPassword,
						HttpClientParams.ntlmHost, HttpClientParams.ntlmDomain);

				// client.getState().setCredentials(new
				// AuthScope(HttpClientParams.ntlmHost, AuthScope.ANY_PORT),
				// ntCreds);
				client.getState().setProxyCredentials(AuthScope.ANY, ntCreds);
				;

				if (log.isInfoEnabled()) {
					log.info("Added NTLM credentials for "
							+ HttpClientParams.ntlmUsername);
				}
			}
		}

		if (log.isInfoEnabled()) {
			log.info("Configured Client");
		}
	}
}
